package com.liang.spring.test.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.liang.spring.test.controller.entity.BuyTicketInfo;
import com.liang.spring.test.controller.entity.BuyTicketInfoExample;
import com.liang.spring.test.controller.entity.TrainInfoExample;
import com.liang.spring.test.controller.entity.BuyTicketInfoExample.Criteria;
import com.liang.spring.test.controller.entity.UserInfo;
import com.liang.spring.test.dao.BuyTicketInfoMapper;

@Controller
@RequestMapping(value = "ticketinfo")
public class ticketController {

	@Autowired
	private BuyTicketInfoMapper buyticketinfomapper;

	@RequestMapping(value = "/add")
	public String addTrainInfo(Model model, BuyTicketInfo buyticketinfo,
			HttpServletRequest request, HttpSession session, PrintWriter printWriter) {
		try {
			String fusername = ((UserInfo) session.getAttribute("user")).getFusername();
			String fcertificateno = ((UserInfo) session.getAttribute("user")).getFcertificateno();
			int count = buyticketinfomapper.selCount(fusername, fcertificateno, buyticketinfo.getFtrainnumber());
			if (count == 0) {
				buyticketinfo.setFusername(fusername);
				buyticketinfo.setFcertificateno(fcertificateno);
				buyticketinfo.setFip(getIpAddr(request));
				buyticketinfomapper.insert(buyticketinfo);
			} 
			String jsonString = JSON.toJSONString(count == 0 ? "成功" : "失败");  
	        printWriter.write(jsonString);  
	        printWriter.flush();  
	        printWriter.close();  
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
		return "/buyTicket";
	}
	
	@RequestMapping(value = "/selAll")
	public String selAll(Model model, HttpSession session){
		try {		
			BuyTicketInfoExample example = new BuyTicketInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andFusernameEqualTo(((UserInfo) session.getAttribute("user")).getFusername());
			criteria.andFcertificatenoEqualTo(((UserInfo) session.getAttribute("user")).getFcertificateno());
			List<BuyTicketInfo> ticketList = buyticketinfomapper.selectByExample(example);
			model.addAttribute("ticketList", ticketList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/myticket";
	}
	
	@RequestMapping(value = "/update")
	public String updatebuyticketinfo(String trainnumber, HttpSession session){
		try {		
			BuyTicketInfoExample example = new BuyTicketInfoExample();
			String fusername = ((UserInfo) session.getAttribute("user")).getFusername();
			String fcertificateno = ((UserInfo) session.getAttribute("user")).getFcertificateno();
			buyticketinfomapper.updatePay(fusername, fcertificateno, trainnumber, example);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return  "redirect:selAll";		//完成此Controller后调用另外一个Controller
	}
	
	@RequestMapping(value = "/del")
	public String delbuyticketinfo(String trainnumber, HttpSession session){
		try {		
			BuyTicketInfoExample example = new BuyTicketInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andFtrainnumberEqualTo(trainnumber);
			criteria.andFusernameEqualTo(((UserInfo) session.getAttribute("user")).getFusername());
			criteria.andFcertificatenoEqualTo(((UserInfo) session.getAttribute("user")).getFcertificateno());
			buyticketinfomapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return  "redirect:selAll";		//完成此Controller后调用另外一个Controller
	}

	private String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
